<template>
  <div class="container">
    <el-card>
      <el-breadcrumb separator-class="el-icon-arrow-right" v-if="subjects.id">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{
          subjects.id ? subjects.name : ""
        }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 头部 -->
      <el-row>
        <el-col :span="18">
          <el-form class="" ref="subjectFrom" :model="list" label-width="80px">
            <el-col :span="6">
              <el-form-item label="目录名称">
                <el-input v-model="list.directoryName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-select v-model="list.state" placeholder="请选择">
                  <el-option label="已启用" :value="1"> </el-option>
                  <el-option label="已禁用" :value="0"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-form>
          <el-col :span="6" class="left-fot">
            <el-button size="small" class="btn" plain @click="resetInput"
              >清除</el-button
            >
            <el-button
              size="small"
              class="btn"
              type="primary"
              @click="getDirectList"
              >搜索</el-button
            >
          </el-col>
        </el-col>
        <el-col :span="6">
          <el-button
            class="rowRight"
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="addDialogVisible = true"
            >新增学科</el-button
          >
          <el-button
            class="rowRight"
            v-if="subjects.id"
            type="text"
            icon="el-icon-back"
            @click="$router.back()"
            >返回学科</el-button
          >
        </el-col>
      </el-row>
      <!-- 提示信息 -->
      <el-alert
        :title="`数据一共 ${count} 条`"
        :closable="false"
        type="info"
        show-icon
      >
      </el-alert>
      <!-- 表格区域 -->
      <el-table style="width: 100%" :data="directoryList">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期"> 
               <template slot-scope="{ row }">
              <span>{{ row.addDate | parseTime }}</span>
            </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="{ row }">
            {{ row.state === 1 ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button type="text" @click="editState(row)">
              {{ row.state === 1 ? "禁用" : "启用" }}
            </el-button>
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="edit(row)"
            >
              修改
            </el-button>
            <el-button type="text" :disabled="row.state === 1" @click="remove(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
        <PageTool
            :paginationPage="list.page"
            :paginationPagesize="list.pagesize"
            :total="count"
            @pageChange="handleCurrentChange"
            @pageSizeChange="handleSizeChange"
            style="float:right"
          ></PageTool>
    </el-card>
    <!-- 添加目录弹框 -->
    <el-dialog title="新增目录" :visible="addDialogVisible" @close="restDir">
      <el-form
        :model="directorysForm"
        :rules="directorysFormRules"
        ref="directorysForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item v-if="!subjects.id"  label="所属学科">
          <el-select
            v-model="directorysForm.value"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in subList"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="目录名称" prop="directoryName">
          <el-input
            v-model="directorysForm.directoryName"
            placeholder="请输入目录名称"
            style="width: 100%"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDirectory">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改目录弹框 -->
    <el-dialog title="修改目录" :visible="editDialogVisible" @close="editDialogVisible = false">
      <el-form
        :model="editDirectoryList"
        :rules="directorysFormRules"
        ref="directorysForm"
        label-width="80px"
      >
        <el-form-item v-if="!subjects.id"  label="所属学科">
          <el-select
            v-model="editDirectoryList.value"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in subList"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="目录名称" prop="directoryName">
          <el-input
            v-model="editDirectoryList.directoryName"
            placeholder="请输入目录名称"
            style="width: 100%"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDirectory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import PageTool from '@/module-dashboard/components/pageTool.vue'
import {
  list as dirList,
  changeState,
  add as addDirectory,
  update as updateDirectory,
  remove as removeDirectory
} from '@/api/hmmm/directorys'
import { simple as simpleSubList } from '@/api/hmmm/subjects'
export default {
  components: { PageTool },
  data () {
    return {
      list: {
        directoryName: '',
        state: null,
        page: 1,
        pagesize: 10
      },
      count: 0, // 数据总数
      directoryList: [],
      directorysForm: {
        // 添加目录
        directoryName: '',
        value: ''
      },
      directorysFormRules: {
        // 添加目录验证
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      addDialogVisible: false, // 新增学科弹层
      subList: [], // 简单学科列表
      editDirectoryList: {
        // 修改目录
        directoryName: '',
        value: ''
      },
      editDialogVisible: false, // 修改弹层
      rowid: '' // id
    }
  },
  created () {
    this.getDirectList()
    this.getAddFrom()
  },
  // 获取地址栏数据
  computed: {
    subjects () {
      return this.$route.query || null
    }
  },

  methods: {
    // 请求目录列表
    async getDirectList () {
      try {
        // this.queryList.subjectID = this.subjects.id || null
        const { data } = await dirList(this.list)
        this.directoryList = data.items
        // console.log(this.directoryList)
        this.count = data.counts
      } catch (error) {
        this.$message.error('请求失败')
      }
    },
    // 清空数据
    resetInput () {
      this.queryList.directoryName = ''
      this.queryList.state = null
      this.getDirectList()
    },
    // 状态切换
    async editState (row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        this.$message.success('操作成功')
        row.state = row.state === 1 ? 0 : 1
      } catch (e) {
        this.$message.error('请求错误!')
      }
    },
    // 修改
    edit (row) {
      this.rowid = row.id
      this.editDirectoryList.value = row.subjectID
      this.editDirectoryList.directoryName = row.directoryName
      this.editDialogVisible = true
    },
    // 删除
    async remove (row) {
      console.log(row)
      try {
        await this.$confirm('此操作将永久删除该学科, 是否继续?')
        await removeDirectory(row)
        this.$message.success('删除成功!')
        this.getDirectList()
      } catch (error) {}
    },
    // 获取学科简单列表
    async getAddFrom () {
      try {
        const { data } = await simpleSubList()
        this.subList = data
      } catch (e) {
        console.log(e)
      }
    },
    // 关闭弹层
    restDir () {
      this.$refs.directorysForm.resetFields()
      this.directorysForm.value = ''
      this.addDialogVisible = false
    },
    // 提交添加
    async addDirectory () {
      try {
        await addDirectory({
          subjectID: this.directorysForm.value,
          directoryName: this.directorysForm.directoryName
        })
        this.$message.success('新增目录成功')
        this.addDialogVisible = false
        this.getDirectList()
      } catch (e) {
        this.$message.error('新增目录失败')
        console.log(e)
      }
    },
    // 提交修改
    async editDirectory () {
      try {
        await updateDirectory({
          id: this.rowid,
          subjectID: this.editDirectoryList.value,
          directoryName: this.editDirectoryList.directoryName
        })
        this.$message.success('修改目录成功')
        this.editDialogVisible = false
        this.getDirectList() // 重新渲染列表
      } catch (error) {
        console.log(error)
      }
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.list.pagesize = val
      if (this.list.page === 1) {
        this.getDirectList(this.list)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.list.page = val
      this.getDirectList()
    }
  }
}
</script>

<style scoped>
.btn {
  height: 36px;
}
.rowRight {
  float: right;
  height: 35px;
  margin-left: 10px;
}
</style>
